import Vue from 'vue'
import VueRouter from 'vue-router'

// 路由懒加载注册方式
const Login = () =>
  import(/* webpackChunkName : "login_home_welcome" */ '../views/Login.vue')
const Home = () =>
  import(/* webpackChunkName : "login_home_welcome" */ '../views/Home.vue')
const Welcome = () =>
  import(/* webpackChunkName : "login_home_welcome" */ '../views/Home.vue')

const GoodsList = () =>
  import(/* webpackChunkName : "goods" */ '../views/goods/GoodsList.vue')
const Cate = () =>
  import(/* webpackChunkName : "goods" */ '../views/goods/Categories.vue')
const Params = () =>
  import(/* webpackChunkName : "goods" */ '../views/goods/Params.vue')
const AddGoodsPage = () =>
  import(/* webpackChunkName : "goods" */ '../views/goods/AddGoodsPage.vue')
const EditGoodsPage = () =>
  import(/* webpackChunkName : "goods" */ '../views/goods/EditGoodsPage.vue')
const AddSuccess = () =>
  import(/* webpackChunkName : "goods" */ '../views/goods/AddSuccess.vue')

const Rights = () =>
  import(/* webpackChunkName : "rights" */ '../views/rights/Rights.vue')
const Roles = () =>
  import(/* webpackChunkName : "rights" */ '../views/rights/Roles.vue')

const User = () =>
  import(
    /* webpackChunkName : "user_orders_report" */ '../views/users/User.vue'
  )
const Orders = () =>
  import(
    /* webpackChunkName : "user_orders_report" */ '../views/order/Orders.vue'
  )
const Report = () =>
  import(
    /* webpackChunkName : "user_orders_report" */ '../views/report/Report.vue'
  )
Vue.use(VueRouter)
const routes = [
  { path: '/', redirect: '/login' },
  {
    path: '/login',
    component: Login,
    meta: { title: '登陆页面' },
    name: 'login'
  },
  {
    path: '/home',
    component: Home,
    meta: { title: '后台主页' },
    name: 'home',
    redirect: '/home/users',
    children: [
      {
        path: 'welcome',
        component: Welcome,
        meta: { title: '欢迎使用后台管理' },
        name: 'welcome'
      },
      {
        path: 'users',
        component: User,
        meta: { title: '用户管理页面' },
        name: 'users'
      },
      {
        path: 'rights',
        component: Rights,
        meta: { title: '权限管理页面' },
        name: 'rights'
      },
      {
        path: 'roles',
        component: Roles,
        meta: { title: '权限管理页面' },
        name: 'roles'
      },
      {
        path: 'categories',
        component: Cate,
        meta: { title: '商品分类页面' },
        name: 'categories'
      },
      {
        path: 'params',
        component: Params,
        meta: { title: '分类参数页面' },
        name: 'params'
      },
      {
        path: 'goods',
        component: GoodsList,
        meta: { title: '商品列表页面' },
        name: 'goods'
      },
      {
        path: 'goods/add',
        component: AddGoodsPage,
        meta: { title: '商品列表页面' },
        name: 'add'
      },

      {
        path: 'goods/edit',
        component: EditGoodsPage,
        meta: { title: '商品列表页面' },
        name: 'edit'
      },
      {
        path: 'goods/addsuccess',
        component: AddSuccess,
        name: 'addsuccess'
      },

      {
        path: 'orders',
        component: Orders,
        meta: { title: '订单列表页面' },
        name: 'orders'
      },

      {
        path: 'reports',
        component: Report,
        meta: { title: '数据报表页面' },
        name: 'reports'
      }
    ]
  }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  if (to.path !== '/login') {
    const token = sessionStorage.getItem('token')
    if (token) {
      next()
    } else {
      next('/login')
    }
  } else {
    next()
  }
})
router.afterEach((to, from) => {
  document.title = to.meta.title
})
export default router
